package com.xusheng.cli.controller.user;

import cn.hutool.core.lang.Dict;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xusheng.cli.common.base.Result;
import com.xusheng.cli.entity.frontUserModule.FrontUser;
import com.xusheng.cli.entity.paymentChannelModule.PaymentChannel;
import com.xusheng.cli.entity.withdrawModule.Withdraw;
import com.xusheng.cli.req.PayReq;
import com.xusheng.cli.service.FrontUserService;
import com.xusheng.cli.service.PaymentChannelService;
import com.xusheng.cli.service.WithdrawService;
import com.xusheng.cli.tools.CurrentPlayerUserUtil;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author lyh
 * @since 2023-04-27 14:57
 */
@RestController
@RequestMapping("/user/v1/payment")
public class PaymentController {

    @Resource
    private PaymentChannelService channelService;
    @Resource
    private FrontUserService frontUserService;
    @Resource
    private WithdrawService withdrawService;

    /**
     * 获取支付通道
     *
     * @return 返回结果
     */
    @GetMapping("/channel")
    public Result<List<Map<String, Object>>> channelList() {
        List<PaymentChannel> channels = channelService.list(Wrappers.<PaymentChannel>lambdaQuery()
                .eq(PaymentChannel::getChannel, 0)
                .eq(PaymentChannel::getApiType, 1)
                .eq(PaymentChannel::getDefaultChannel, true));
        return Result.success(channels.stream()
                .map(c -> Dict.create()
                        .set("id", c.getId())
                        .set("name", c.getName())
                        .set("type", c.getType())
                        .set("rate", c.getRate()))
                .collect(Collectors.toList()));
    }

    /**
     * app支付通道
     *
     * @return
     */
    @GetMapping("/appChannel")
    public Result<List<Map<String, Object>>> appChannelList() {
        List<PaymentChannel> channels = channelService.list(Wrappers.<PaymentChannel>lambdaQuery()
                .eq(PaymentChannel::getChannel, 1)
                .eq(PaymentChannel::getApiType, 1)
                .eq(PaymentChannel::getDefaultChannel, true));
        return Result.success(channels.stream()
                .map(c -> Dict.create()
                        .set("id", c.getId())
                        .set("name", c.getName())
                        .set("type", c.getType())
                        .set("rate", c.getRate()))
                .collect(Collectors.toList()));
    }

    public Object pay(@Validated @RequestBody PayReq rechargeInfoReq, HttpServletRequest request) {
        //TODO
        return null;
    }


    /**
     * 提现
     *
     * @param withdraw
     * @return
     */
    @PostMapping("withdraw")
    public Result<Object> withdraw(@RequestBody Withdraw withdraw) {
        FrontUser frontUser = frontUserService.getById(CurrentPlayerUserUtil.getUserId());
        withdraw.setUserId(CurrentPlayerUserUtil.getUserId());
        withdraw.setState(1);
        withdraw.setType(2);
        withdraw.setAccountType(2);
        withdraw.setChannel(2);
        // 玩家计算剩余金币
        frontUser.setGold(frontUser.getGold() - withdraw.getAmount().multiply(new BigDecimal(10)).intValue());
        frontUserService.updateById(frontUser);
        return Result.isSuccess(withdrawService.save(withdraw));
    }
}
